package com.third.clock.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bigkoo.svprogresshud.SVProgressHUD;
import com.lzy.okgo.model.Response;
import com.third.clock.R;
import com.third.clock.bean.ResponseData;
import com.third.clock.common.Contants;
import com.third.clock.manager.ClockManager;
import com.third.clock.manager.OkHttpClientManager;
import com.third.clock.manager.TransformManager;
import com.third.clock.manager.WindowMessageManager;
import com.third.clock.ui.view.CodeView;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2018/8/2 0002.
 */

public class ValidateCodeActivity extends Activity implements View.OnClickListener{

    private EditText mEditTextName;

    private EditText mEditTextCode;

    private TextView mTextView;

    private ImageView mImageViewCode;

    private ImageView mImageNameClear;

    private Button mButtonalidatecode;

    private CodeView mCodeView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_validatecode);
        init();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        WindowMessageManager.getInstance().dismiss();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.textview_code:
            case R.id.imageview_code:
                getcode();
                break;
            case R.id.btn_validatecode:
                validatecode();
                break;
            case R.id.iv_unameClear:
                mEditTextName.setText("");
                break;
        }
    }

    private void init(){
        mEditTextName= (EditText) findViewById(R.id.et_userName);
        mEditTextCode= (EditText) findViewById(R.id.et_code);
        mTextView= (TextView) findViewById(R.id.textview_code);
        mImageViewCode= (ImageView) findViewById(R.id.imageview_code);
        mButtonalidatecode=(Button) findViewById(R.id.btn_validatecode);
        mImageNameClear=(ImageView) findViewById(R.id.iv_unameClear);

        mCodeView=new CodeView();

        mTextView.setOnClickListener(this);
        mImageViewCode.setOnClickListener(this);
        mButtonalidatecode.setOnClickListener(this);
        mImageNameClear.setOnClickListener(this);
    }

    private void getcode(){
        ClockManager clockManager=ClockManager.getInstace(ValidateCodeActivity.this);
        if(clockManager!=null){
            OkHttpClientManager okHttpClientManager=clockManager.getOkHttpClientManager();
            if(okHttpClientManager!=null){
                String mobile=mEditTextName.getText().toString().trim();
                if(TextUtils.isEmpty(mobile)){
                    showInfoWithStatus("用户名不为空");
                    return;
                }
                if(!TransformManager.isMobileNO(mobile)){
                    showInfoWithStatus("请输入正确的手机号");
                    return;
                }
                Map<String,Object> sendMap=new HashMap<>();
                sendMap.put("mobile",mobile);

                String toSign=TransformManager.sortMapByKey(sendMap);
                sendMap.put("sign",toSign);

                okHttpClientManager.post(Contants.SENDCODE_URL, sendMap, new OkHttpClientManager.okHttpCallback() {
                    @Override
                    public void onFailure(Response<String> s) {
                        showInfoWithStatus("获取验证码失败");
                    }

                    @Override
                    public void onResponse(Response<String> response) {
                         if(response!=null&&response.body()!=null){
                             ResponseData<String> coderesponse=TransformManager.paserString(response.body());
                             if(coderesponse!=null&&coderesponse.getCode()==Contants.CODE_0){
                                 showcode(coderesponse.getData());
                             }else{
                                 showInfoWithStatus("获取验证码失败");
                             }
                         }else{
                             showInfoWithStatus("获取验证码失败");
                         }
                    }
                });

            }
        }
    }

    private void validatecode(){
        ClockManager clockManager=ClockManager.getInstace(ValidateCodeActivity.this);
        if(clockManager!=null){
            OkHttpClientManager okHttpClientManager=clockManager.getOkHttpClientManager();
            if(okHttpClientManager!=null){
                String mobile=mEditTextName.getText().toString().trim();
                if(TextUtils.isEmpty(mobile)){
                    showInfoWithStatus("用户名不为空");
                    return;
                }
                if(!TransformManager.isMobileNO(mobile)){
                    showInfoWithStatus("请输入正确的手机号");
                    return;
                }
                String code=mEditTextCode.getText().toString().trim();
                if(TextUtils.isEmpty(mobile)){
                    showInfoWithStatus("校验码不能为空");
                    return;
                }

                Map<String,Object> sendMap=new HashMap<>();
                sendMap.put("mobile",mobile);
                sendMap.put("code",code);

                String toSign=TransformManager.sortMapByKey(sendMap);
                sendMap.put("sign",toSign);

                okHttpClientManager.post(Contants.VALIDATECODE_URL, sendMap, new OkHttpClientManager.okHttpCallback() {
                    @Override
                    public void onFailure(Response<String> s) {
                        showInfoWithStatus("验证失败");
                    }

                    @Override
                    public void onResponse(Response<String> response) {
                        if(response!=null&&response.body()!=null){
                            ResponseData<String> coderesponse=TransformManager.paserString(response.body());
                            if(coderesponse!=null&&coderesponse.getCode()==Contants.CODE_0){
                                start();
                            }else{
                                showInfoWithStatus("验证失败");
                            }
                        }else{
                            showInfoWithStatus("验证失败");
                        }
                    }
                });

            }
        }
    }

    private void start(){
        new Handler().post(new Runnable() {
            @Override
            public void run() {
                ValidateCodeActivity.this.startActivity(new Intent(ValidateCodeActivity.this,EditPassWordActivity.class));
                ValidateCodeActivity.this.finish();
            }
        });

    }

    private void showcode(String code){
        if(!TextUtils.isEmpty(code)){
           if(mTextView!=null) mTextView.setVisibility(View.GONE);
           if(mImageViewCode!=null) {
               mImageViewCode.setVisibility(View.VISIBLE);
               mCodeView.setCode(code);
               mImageViewCode.setImageBitmap(mCodeView.getBitmap());
           }
        }
    }

    private void showInfoWithStatus(String string){
        if(!TextUtils.isEmpty(string)) {
            WindowMessageManager.getInstance().showInfoWithStatus(ValidateCodeActivity.this,string);
        }
    }

}
